var app = {
	description : 'App Common JS Library',
	version : '0.0.1'
};

app.login = {
	doLogin : function($form, callback) {
		var userName = $form.find('input[name=userName]').val();
		var password = $form.find('input[name=password]').val();
		$.ajax({
			type: "POST",
			url: webroot + "/system/userLogin",
			data: {
				userName:userName,
    	 		password:password
    	 	},
     		dataType: "json",
     		success: function(resp){
				if(resp.success){
					if (resp.content && resp.content.redirect) {
						location.href = webroot + resp.content.redirect;
					}
				} else {
					alert(resp.message);
				}
        	}
         });
	}
};


//格式化日期：年-月-日
app.inputDate = {
	myformatter : function(date) {
		var y = date.getFullYear();
		var m = date.getMonth() + 1;
		var d = date.getDate();
		return y + '-' + (m < 10 ? ('0' + m) : m) + '-'
				+ (d < 10 ? ('0' + d) : d);
	},

	myparser : function(s) {
		if (!s)
			return new Date();
		var ss = (s.split('-'));
		var y = parseInt(ss[0], 10);
		var m = parseInt(ss[1], 10);
		var d = parseInt(ss[2], 10);
		if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
			return new Date(y, m - 1, d);
		} else {
			return new Date();
		}
	},
	myformatterTime : function(date) {
		var y = date.getFullYear();
		var m = date.getMonth() + 1;
		var d = date.getDate();
		var h = date.getHours();
		var i = date.getMinutes();
		var s = date.getSeconds();
		return y + '-' + (m < 10 ? ('0' + m) : m) + '-'
				+ (d < 10 ? ('0' + d) : d) + ' ' + (h < 10 ? ('0' + h) : h)
				+ ':' + (i < 10 ? '0' + i : i) + ':' + (s < 10 ? '0' + s : s);
	}
};

app.print = {
	msg : function(msg,index,callback){
		layer.msg(msg,{time:1500},function(){
			if(callback != null && callback!=undefined){
				callback;
			}
			if(index != null && index!=undefined){
				parent.layer.close(index);
			}
		});
	},
	success : function(msg,index,callback){
		layer.msg(msg,{time:1500,icon: 1},function(){
			if(callback != null && callback!=undefined){
				callback;
			}
			if(index != null && index!=undefined){
				parent.layer.close(index);
			}
		});
	},
	error : function(msg,index,callback){
		layer.msg(msg,{time:1500,icon: 2},function(){
			if(callback != null && callback!=undefined){
				callback;
			}
			if(index != null && index!=undefined){
				parent.layer.close(index);
			}
		});
	},
	confirm : function(msg,success,error){
		layer.confirm(msg, { btn: ['确定','取消']},success,error);
	}
}

app.dialog = {
		modal : function(myModalLabel,body){
			$("#myModalLabel").text(myModalLabel);
			$("#modal-body").text(body);
		}
}

app.upload = {
		init : function(butId,fileType,showId,successCallBack,failedCallback){
			// 实例化
		    uploader = WebUploader.create({
		        pick: {
		            id: '#' + butId,
		            label: '点击选择'
		        },

		        accept: {
		            title: 'file',
		            extensions: fileType,
		            mimeTypes: 'image/*'
		        },
		        auto: true,
		        swf: webroot + '/static/webuploader/Uploader.swf',
		        server: webroot + '/fileupload/upload',
		        resize: false
		    });
		    
		    uploader.on( 'fileQueued', function( file ) {
		    	var html =  '<div id="' + file.id + '" class="item">' +
		            			'<h4 class="info">' + file.name + '</h4>' +
		            			'<p class="state">等待上传...</p>' +
	            			'</div>'
		    	$("#" + showId).html(html);
		    });
		    
		    uploader.on( 'uploadProgress', function( file, percentage ) {
		        var $li = $( '#'+file.id ),
		            $percent = $li.find('.progress .progress-bar');
		        // 避免重复创建
		        if ( !$percent.length ) {
		            $percent = $('<div class="progress progress-striped active">' +
		              '<div class="progress-bar" role="progressbar" style="width: 0%">' +
		              '</div>' +
		            '</div>').appendTo( $li ).find('.progress-bar');
		        }

		        $li.find('p.state').text('上传中');

		        $percent.css( 'width', percentage * 100 + '%' );
		    });
		    
		    uploader.on( 'uploadSuccess', function( file,data ) {
		    	$( '#'+file.id ).find('p.state').text('已上传');
		    	successCallBack(data);
		    });

		    uploader.on( 'uploadError', function( file,data ) {
		    	alert(data);
		    	 $( '#'+file.id ).find('p.state').text('上传出错');
		    	failedCallback(data);
		    });

		},
		type : {
			image : function(){
				return 'gif,jpg,jpeg,bmp,png';
			}
		}
}
